package cn.wjx.taobaounion.ui.fragment;

import android.content.Intent;
import android.graphics.Rect;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;

import butterknife.BindView;
import cn.wjx.taobaounion.R;
import cn.wjx.taobaounion.base.BaseFragment;
import cn.wjx.taobaounion.model.domain.OnSaleContent;
import cn.wjx.taobaounion.presenter.IOnSalePresenter;
import cn.wjx.taobaounion.presenter.ITicketPresenter;
import cn.wjx.taobaounion.ui.activity.TicketActivity;
import cn.wjx.taobaounion.ui.adapter.OnSalePageAdapter;
import cn.wjx.taobaounion.utils.PresenterManger;
import cn.wjx.taobaounion.utils.SizeUtils;
import cn.wjx.taobaounion.utils.ToastUtil;
import cn.wjx.taobaounion.view.IOnSalePageCallback;

/**
 * @author WuChangJian
 * @date 2020/6/16 21:07
 */
public class OnSaleFragment extends BaseFragment implements IOnSalePageCallback, OnSalePageAdapter.OnSalePageItemClickListener {


    private static final int DEFAULT_SPAN_COUNT = 2;
    private IOnSalePresenter mOnSalePresenter;
    @BindView(R.id.on_sale_recycler)
    public RecyclerView mSaleRecycler;
    @BindView(R.id.on_sale_refresh)
    public TwinklingRefreshLayout mRefreshLayout;
    @BindView(R.id.bar_title)
    public TextView barTitle;

    private OnSalePageAdapter mOnSalePageAdapter;

    @Override
    protected int getRootViewResId() {
        return R.layout.fragment_on_sale;
    }

    @Override
    protected View loadRootView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_with_bar_layout, container, false);
    }

    @Override
    protected void initView(View rootView) {
        barTitle.setText(getResources().getString(R.string.text_on_sale_bar));
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), DEFAULT_SPAN_COUNT);
        mSaleRecycler.setLayoutManager(gridLayoutManager);
        mOnSalePageAdapter = new OnSalePageAdapter();
        mSaleRecycler.setAdapter(mOnSalePageAdapter);
        mSaleRecycler.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                outRect.left = SizeUtils.dip2px(getContext(), 2.5f);
                outRect.right = SizeUtils.dip2px(getContext(), 2.5f);
                outRect.bottom = SizeUtils.dip2px(getContext(), 2.5f);
                outRect.top = SizeUtils.dip2px(getContext(), 2.5f);
            }
        });
        mRefreshLayout.setEnableRefresh(false);
        mRefreshLayout.setEnableLoadmore(true);
    }

    @Override
    protected void initPresenter() {
        mOnSalePresenter = PresenterManger.getInstance().getOnSalePresenter();
        mOnSalePresenter.registerViewCallback(this);
        mOnSalePresenter.getContent();
    }

    @Override
    protected void initListener() {
        mRefreshLayout.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
                if (mOnSalePresenter != null) {
                    mOnSalePresenter.loadMore();
                }
            }
        });
        mOnSalePageAdapter.setOnSalePageItemClickListener(this);
    }

    @Override
    public void onContentLoaded(OnSaleContent content) {
        //LogUtils.d(this, "on sale content-->" + content);
        setUpState(State.SUCCESS);
        if (mOnSalePageAdapter != null) {
            mOnSalePageAdapter.setData(content);
        }
    }

    @Override
    public void onLoaderMoreLoaded(OnSaleContent content) {
        int size = content.getData().getTbk_dg_optimus_material_response().getResult_list().getMap_data().size();
        ToastUtil.showToast("加载了" + size + "条数据");
        if (mRefreshLayout != null) {
            mRefreshLayout.finishLoadmore();
        }
        if (mOnSalePageAdapter != null) {
            mOnSalePageAdapter.loaderData(content);
        }
    }

    @Override
    public void onLoaderMoreError() {
        if (mRefreshLayout != null) {
            mRefreshLayout.finishLoadmore();
        }
        ToastUtil.showToast("加载数据失败,请稍候重试");
    }

    @Override
    public void onLoaderMoreEmpty() {
        if (mRefreshLayout != null) {
            mRefreshLayout.finishLoadmore();
        }
        ToastUtil.showToast("加载数据为空");
    }

    @Override
    public void reload() {
        // 网络错误加载重试
        if (mOnSalePresenter != null) {
            mOnSalePresenter.reload();
        }
    }

    @Override
    public void onError() {
        setUpState(State.ERROR);
    }

    @Override
    public void onLoading() {
        setUpState(State.LOADING);
    }

    @Override
    public void onEmpty() {
        setUpState(State.EMPTY);
    }

    @Override
    protected void release() {
        if (mOnSalePresenter != null) {
            mOnSalePresenter.unregisterViewCallback(this);
        }
    }

    @Override
    public void onSaleItemClick(OnSaleContent.DataBean.TbkDgOptimusMaterialResponseBean.ResultListBean.MapDataBean item) {
        // 条目被点击，跳转到淘口令界面
        String title = item.getTitle();
        // 领券的Url
        String url = item.getCoupon_click_url();
        if (TextUtils.isEmpty(url)) {
            // 若没有优惠券，则跳转到商品详情
            url = item.getClick_url();
        }
        String cover = item.getPict_url();
        ITicketPresenter ticketPresenter = PresenterManger.getInstance().getTicketPresenter();
        ticketPresenter.getTicket(title, cover, url);
        Intent intent = new Intent(getContext(), TicketActivity.class);
        intent.putExtra("title", title);
        intent.putExtra("url", url);
        intent.putExtra("cover", cover);
        startActivity(intent);
    }
}
